package com.uduemc.biso.master.backend.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.github.pagehelper.PageInfo;
import com.uduemc.biso.core.common.dto.CBNodemanageSiteList;
import com.uduemc.biso.core.common.extities.CHostData;
import com.uduemc.biso.core.common.feign.CHostFeign;
import com.uduemc.biso.core.extities.center.Host;
import com.uduemc.biso.core.extities.center.SysHostType;
import com.uduemc.biso.core.extities.center.SysLanguage;
import com.uduemc.biso.core.feign.HostFeign;
import com.uduemc.biso.core.feign.SysHostTypeFeign;
import com.uduemc.biso.core.feign.SysLanguageFeign;
import com.uduemc.biso.core.utils.RestResult;
import com.uduemc.biso.core.utils.ResultUtil;
import com.uduemc.biso.master.backend.service.HostService;

@Service
public class HostServiceImpl implements HostService {

	@Autowired
	private SysHostTypeFeign sysHostTypeFeign;

	@Autowired
	private SysLanguageFeign sysLanguageFeign;

	@Autowired
	private CHostFeign cHostFeign;

	@Autowired
	private HostFeign hostFeign;

	@Override
	public List<SysHostType> getHostType()
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = sysHostTypeFeign.findAll();
		@SuppressWarnings("unchecked")
		List<SysHostType> data = (ArrayList<SysHostType>) ResultUtil.data(restResult,
				new TypeReference<ArrayList<SysHostType>>() {
				});
		return data;
	}

	@Override
	public List<SysLanguage> getSysLanguage()
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = sysLanguageFeign.findAll();
		@SuppressWarnings("unchecked")
		List<SysLanguage> data = (ArrayList<SysLanguage>) ResultUtil.data(restResult,
				new TypeReference<ArrayList<SysLanguage>>() {
				});
		return data;
	}

	@Override
	public PageInfo<CHostData> getHostDataList(CBNodemanageSiteList cBNodemanageSiteList)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = cHostFeign.findListInfos(cBNodemanageSiteList);
		@SuppressWarnings("unchecked")
		PageInfo<CHostData> data = (PageInfo<CHostData>) ResultUtil.data(restResult,
				new TypeReference<PageInfo<CHostData>>() {
				});
		return data;
	}

	@Override
	public Host getHost(long id, long agentId)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		Host host = getHost(agentId);
		if (host == null || host.getAgentId() == null) {
			return null;
		}
		if (host.getAgentId().longValue() == agentId) {
			return host;
		}
		return null;
	}

	@Override
	public Host getHost(long id) throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = hostFeign.findOne(id);
		Host data = ResultUtil.data(restResult, Host.class);
		return data;
	}

	@Override
	public Host updateHost(Host host)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		RestResult restResult = hostFeign.updateById(host);
		Host data = ResultUtil.data(restResult, Host.class);
		return data;
	}

}
